import 'package:amoy_ticket/utils/persistent_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import '../../../widget/dialog/show_dialog.dart';

class ModifyNickNameLogic extends GetxController {
  final TextEditingController controller = TextEditingController();
  var ps = PersistentStorage();
  var _map = {};
  @override
  void onInit() {
    getNickName();

    super.onInit();
  }

  getNickName() async {
    try {
      await ps.getStorage('userInfo').then((value) {
        _map = value;

        controller.text = _map['name'];
      });
    } catch (e) {
      controller.text = 'hello';
    }
    update();
  }

  modifyName(context) async {
    ShowDialog.cupertinoDialog(
      context,
      content: '确认将昵称修改为"${controller.text}"?',
      closeFunction: () {
        return 'hello';
      },
      sureFunction: () async {
        _map['name'] = controller.text;
        _map['headImg'] =
            'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/people/dog.jpg';

        await ps.removeStorage('userInfo').then((value) {
          ps.setStorage('userInfo', _map);
        });

        await ps.getStorage('userInfo').then((value) {
          SmartDialog.showToast('修改成功～ 新昵称为"${value['name']}"');
        });
      },
    );
  }

  clearController() {
    controller.clear();
    update();
  }

  changeInPutText(value) {
    controller.text = value;
    update();
  }
}
